package com.linghang.wusthelper.spider.jwc;

import com.linghang.wusthelper.entity.primary.SelectCourseInfo;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

/**
 * @author origin
 * 可选课程列表爬虫
 */
public class SelectCourseListSpider implements PageProcessor {

    private String xklbname;

    private String lock;

    private List<SelectCourseInfo> selectCourseInfoList;

    private Site site = Site.me()
            .setRetryTimes(3)
            .setTimeOut(10000)
            .addHeader("Connection", "keep-alive")
            .addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
            .addHeader("Accept-Encoding", "gzip,deflate,sdch")
            .addHeader("Accept-Language", "zh-CN,zh;q=0.8")
            .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36")
            .addHeader("Cache-Control","no-cache")
            .addHeader("Host","jwxt.wust.edu.cn")
            .setDomain("jwxt.wust.edu.cn");

    public SelectCourseListSpider(String JSESSIONID,String SERVERID,String xklbname,String lock,List<SelectCourseInfo> selectCourseInfoList){
        site.addCookie("JSESSIONID",JSESSIONID);
        site.addCookie("SERVERID",SERVERID);
        this.xklbname = xklbname;
        this.lock = lock;
        this.selectCourseInfoList = selectCourseInfoList;
    }

    @Override
    public void process(Page page) {
        int nums = Integer.valueOf(page.getHtml().xpath("//*[@id=\"PageNavigation\"]/font/text()").get());
        List<String> noList = page.getHtml().xpath("//*[@id=\"1\"]/td[1]/text()").all();
        List<String> courseNameList = page.getHtml().xpath("//*[@id=\"1\"]/td[2]/text()").all();
        List<String> courseUnitList = page.getHtml().xpath("//*[@id=\"1\"]/td[3]/text()").all();
        List<String> directionList = page.getHtml().xpath("//*[@id=\"1\"]/td[4]/text()").all();
        List<String> courseCreditList = page.getHtml().xpath("//*[@id=\"1\"]/td[5]/text()").all();
        List<String> selectNumsList = page.getHtml().xpath("//*[@id=\"1\"]/td[6]/text()").all();
        List<String> marginList = page.getHtml().xpath("//*[@id=\"1\"]/td[7]/text()").all();
        List<String> teacherList = page.getHtml().xpath("//*[@id=\"1\"]/td[8]/@title").all();
        List<String> weeksList = page.getHtml().xpath("//*[@id=\"1\"]/td[9]/text()").all();
        List<String> courseTimeList = page.getHtml().xpath("//*[@id=\"1\"]/td[10]/text()").all();
        List<String> coursePlaceList = page.getHtml().xpath("//*[@id=\"1\"]/td[11]/text()").all();
        List<String> courseCategoryList = page.getHtml().xpath("//*[@id=\"1\"]/td[12]/text()").all();
        List<String> courseNatureList = page.getHtml().xpath("//*[@id=\"1\"]/td[13]/text()").all();
        List<String> groupNameList = page.getHtml().xpath("//*[@id=\"1\"]/td[14]/text()").all();
        List<String> sexList = page.getHtml().xpath("//*[@id=\"1\"]/td[15]/text()").all();
        List<String> urlList = page.getHtml().xpath("//*[@id=\"1\"]/td[16]//a/@onclick").regex("vJsMod\\('(.*?)',").all();

        for (int i = 0;i < nums;i++){
            int no = Integer.valueOf(noList.get(i).replaceAll(" ",""));
            String courseName = courseNameList.get(i);
            String courseUnit = courseUnitList.get(i);
            String direction = directionList.get(i);
            String courseCredit = courseCreditList.get(i);
            String selectedNums = selectNumsList.get(i);
            String margin = marginList.get(i);
            String teacher = teacherList.get(i);
            String weeks = weeksList.get(i);
            String courseTime = courseTimeList.get(i);
            String coursePlace = coursePlaceList.get(i);
            String courseCategory = courseCategoryList.get(i);
            String courseNature = courseNatureList.get(i);
            String groupName = groupNameList.get(i);
            String sex = sexList.get(i);
            String url = urlList.get(i);
            SelectCourseInfo courseInfo = new SelectCourseInfo(no,xklbname,courseName,courseUnit,direction,courseCredit,selectedNums,margin,teacher,weeks,courseTime,coursePlace,courseCategory,courseNature,groupName,sex,url);
            selectCourseInfoList.add(courseInfo);
        }
        synchronized (lock){
            lock.notify();
        }
    }

    @Override
    public Site getSite() {
        return site;
    }
}
